package com.chinafree.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinafree.auth.model.enumeration.Column;
import com.chinafree.business.model.queryCondition.UserQueryCondition;
import com.chinafree.business.service.MaUserEducationService;
import com.chinafree.business.service.UserService;
import com.chinafree.mapper.*;
import com.chinafree.model.po.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.function.Consumer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Autowired
    private MaUserDeliveryMapper maUserDeliveryMapper;

    @Autowired
    private FndProCityAreaMapper fndProCityAreaMapper;

    @Autowired
    private FndPositionMapper fndPositionMapper;

    @Autowired
    private MaUserMapper maUserMapper;

    @Autowired
    private MaUserWorkMapper maUserWorkMapper;

    @Autowired
    private MaUserEducationMapper maUserEducationMapper;

    @Autowired
    private MaUserSkillMapper maUserSkillMapper;

    @Autowired
    private MaUserFileMapper maUserFileMapper;

    @Override
    public Optional<MaUser> getMaUserByUserId(Long userId) {
        MaUser maUser = maUserMapper.selectById(userId);
        if (maUser == null) {
            Optional.empty();
        }
        return Optional.ofNullable(maUser);
    }

    @Override
    public Optional<List<MaUserWork>> getMaUserWorksByUserId(Long userId) {
        // QueryWrapper<MaUserWork> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq("user_id", userId);
        List<MaUserWork> maUserWorks = maUserWorkMapper
                .selectList(new QueryWrapper<MaUserWork>().eq("user_id", userId));
        if (CollectionUtils.isEmpty(maUserWorks)) {
            return Optional.empty();
        }
        return Optional.of(maUserWorks);
    }

    @Override
    public Optional<List<MaUserEducation>> getMaUserEducationsByUserId(Long userId) {
        // QueryWrapper<MaUserEducation> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq(Column.USER_ID.getColumn(), userId);
        List<MaUserEducation> maUserWorks = maUserEducationMapper
                .selectList(new QueryWrapper<MaUserEducation>().eq("user_id", userId).eq("deleted", 0));
        if (CollectionUtils.isEmpty(maUserWorks)) {
            return Optional.empty();
        }
        return Optional.of(maUserWorks);
    }

    @Override
    public Optional<List<MaUserSkill>> getMaUserSkillsByUserId(Long userId) {
        QueryWrapper<MaUserSkill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Column.USER_ID.getColumn(), userId).eq("deleted", 0);
        List<MaUserSkill> maUserWorks = maUserSkillMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(maUserWorks)) {
            return Optional.empty();
        }
        return Optional.of(maUserWorks);
    }

    @Override
    public Optional<List<MaUserFile>> getMaUserFileByUserId(Long userId) {
        QueryWrapper<MaUserFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Column.USER_ID.getColumn(), userId).eq("deleted", 0);
        List<MaUserFile> maUserWorks = maUserFileMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(maUserWorks)) {
            return Optional.empty();
        }
        return Optional.of(maUserWorks);
    }

    @Override
    public void update(MaUser maUser) {
        maUserMapper.updateById(maUser);
    }

    @Override
    public void insert(MaUser maUser) {
        maUserMapper.insert(maUser);
    }

    private MaUserEducationService maUserEducationService;

    @Transactional
    @Override
    public void updateMaUserEducation(List<MaUserEducation> maUserEducations, Long id) {
        HashMap<String, Object> deletedMap = new HashMap<>();
        deletedMap.put("user_id", id);
        maUserEducationMapper.deleteByMap(deletedMap);
        maUserEducationService.saveBatch(maUserEducations);
    }

    @Override
    public void updateMaUserWork(List<MaUserWork> maUserWorks, Long id) {

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("user_id", id);
        maUserWorkMapper.deleteByMap(stringObjectHashMap);
        for (MaUserWork maUserWork : maUserWorks) {

            maUserWorkMapper.insert(maUserWork);

        }
    }

    @Override
    public void updateMaUserSkill(List<MaUserSkill> maUserSkills, Long id) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("user_id", id);
        maUserSkillMapper.deleteByMap(stringObjectHashMap);
        for (MaUserSkill maUserSkill : maUserSkills) {

            maUserSkillMapper.insert(maUserSkill);

        }
    }

    @Override
    public void updateMaUserFile(List<MaUserFile> maUserFiles) {
        for (MaUserFile maUserFile : maUserFiles) {
            if (maUserFile.getId() != null) {
                maUserFileMapper.updateById(maUserFile);
            } else {
                maUserFileMapper.insert(maUserFile);
            }
        }
    }

    @Override
    public String getCityNameById(Integer liveCity) {
        if (liveCity == null) {
            return null;
        }
        FndProvCityArea fndProvCityArea = fndProCityAreaMapper
                .selectOne(new QueryWrapper<FndProvCityArea>().eq("code", liveCity));
        if (fndProvCityArea == null) {
            return "城市对应错误";
        }
        return fndProvCityArea.getName();
    }

    @Override
    public FndPosition getJobTypeById(Integer jobType) {
        if (jobType == null) {
            return null;
        }
        FndPosition fndPosition = fndPositionMapper.selectById(jobType);
        return fndPosition;
    }

    @Override
    public List<FndProvCityArea> getCityAll() {
        return fndProCityAreaMapper.selectList(null);
    }

    @Override
    public List<FndPosition> getJobTypeAll() {
        return fndPositionMapper.selectList(null);
    }

    /**
     * 查询角色列表(分页)
     *
     * @param condition 角色参数
     * @return 查询角色分页列表
     */
    @Override
public Page<MaUser> selectUserListPage(UserQueryCondition condition) {
    Page<MaUser> page = new Page<>(condition.getPageIndex(), condition.getPageSize());
    MaUser maUser = new MaUser();
    BeanUtils.copyProperties(condition, maUser);
    QueryWrapper<MaUser> entityWrapper = new QueryWrapper<>(maUser);
    if(StringUtils.isNotBlank(condition.getText())){
        entityWrapper.like("brief_description",condition.getText() );
    }
    entityWrapper.eq(Column.ENABLE_RESUME.getColumn(), 1);
    entityWrapper.eq(Column.IS_DELETED.getColumn(), 0);
    return maUserMapper.selectPage(page, entityWrapper);
}

    @Override
    public Optional<List<MaUser>> selectByUser(MaUser maUser) {
        List<MaUser> list = maUserMapper.selectList(new QueryWrapper<MaUser>(maUser));
        if (list == null || list.size() == 0) {

            Optional.empty();
        }
        return Optional.ofNullable(list);
    }

    @Override
    @Transactional
    public void updateUserDelivery(Long userId, Long company, MaUserDelivery ma) {
        Map map = new HashMap<>();
        map.put(Column.USER_ID.getColumn(),userId);
        List l = maUserDeliveryMapper.selectByMap(map);
        if(CollectionUtils.isNotEmpty(l)){  
            maUserDeliveryMapper.update(ma, new UpdateWrapper<MaUserDelivery>().allEq(map)  );
        } else {
            ma.setUserId(userId);
            
            // maUserDeliveryMapper.insert();
        }
        return ;
    }

}
